package com.yuelao.controller.login;

import com.yuelao.entity.User;
import com.yuelao.mapper.UserMapper;
import com.yuelao.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登陆
 * @author FLY
 * @version v1.0
 * @project yuelao-server
 * @date 2017/8/29 14:13
 * @修改记录
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 */
@Slf4j
@Controller
@RequestMapping(value = "yuelao/{version}")
public class LoginController {
    @Autowired
    private UserMapper userMapper;
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "login")
    public void login() {
        Map<String, String> resMap = new HashMap();
        log.info("login########");
        resMap.put("name", "fengliye");
        String aaa= resMap.get("hhhhh");
        throw new RuntimeException("");
    }

    @RequestMapping(method = RequestMethod.GET, value = "downLoad")
    public void downLoad(HttpServletResponse response) {
        List<User> users = userMapper.selectAll();
        String[] rowName = {"","ID","姓名", "时间"};
        List<Object[]> listData = new ArrayList<>();
        users.forEach(u ->{
            Object[] userArr = new Object[3];
            userArr[0] = u.getId();
            userArr[1] = u.getName();
            userArr[2] = "shijain";
            listData.add(userArr);
        });
        try {
            HSSFWorkbook hssfWorkbook = null;
            hssfWorkbook = ExcelUtil.generBook("导出测试", rowName, listData);
            ExcelUtil.resExcel(response, hssfWorkbook, "导出测试");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
